import Hammer from "hammerjs";
import { Canvas } from "../canvas/Canvas";
import { UserHub } from "./UserListener";
export class TouchBind {
    touch = new Hammer.Manager(document.body);
    constructor() {
        this.rebuild();
    }

    rebuild() {
        this.touch.destroy();
        this.ScaleStop && this.ScaleStop();
        this.touch = new Hammer.Manager(document.body);
        const pinch = new Hammer.Pinch();
        this.touch.add([pinch]);
    }

    private ScaleStop?: Function;
    bindScale(canvas: Canvas) {
        let originScale = canvas.scale;
        const events = [
            ["pinchstart", () => (originScale = canvas.scale)],
            [
                "pinchmove",
                (env: HammerInput) => {
                    canvas.scale = env.scale * originScale;
                    UserHub.emit("scale", canvas.scale);
                },
            ],
        ] as [string, HammerListener][];
        events.forEach(([name, event]) => {
            this.touch.on(name, event);
        });
        this.ScaleStop = () => {
            events.forEach(([name, event]) => {
                this.touch.off(name, event);
            });
        };
        return this.ScaleStop;
    }
}
